/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Locale;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import sunsite.po.Usr;
import sunsite.service.UserService;
import sunsite.tools.ContextHelper;
import sunsite.tools.Json;
import sunsite.tools.Naming;
import sunsite.vo.UserInfo;

/**
 *
 * @author Administrator
 */
public class Login extends ActionSupport {

    //请求参数
    private String userName;
    private String password;
    private String verifyCode;
    
    //传递参数
    private String message;
    
    private ResourceBundleMessageSource messageSource;
    private UserService userService;
    public static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Login.class);

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public String execute() throws Exception {
        Json json = new Json();

        String verifyCodeSession = (String)ServletActionContext.getRequest().getSession().getAttribute(sunsite.tools.Naming.SessionName.VerifyCodeSession);
        if(getVerifyCode()==null||!verifyCode.equals(verifyCodeSession))
        {
            json.add(Json.Answer, Json.ModalMessageBox);
            json.add(Json.MsgTitle, messageSource.getMessage("message.errorMsgTitle", null, Locale.getDefault()));
            json.add(Json.MsgContent, messageSource.getMessage("user.checkCodeError", null, Locale.getDefault()));
            message=json.toString();
            return SUCCESS;
        }
        
        Usr user = null;
        try
        {
            user = userService.validate(userName,password);
        }
        catch(Exception err)
        {
            logger.error(err);
            json.add(Json.Answer, Json.ModalMessageBox);
            json.add(Json.MsgTitle, messageSource.getMessage("message.errorMsgTitle", null,Locale.getDefault()));
            json.add(Json.MsgContent, err.toString());
            message=json.toString();
            return SUCCESS;
        }
        
        if(user != null)//"登录成功"
        {
            //写入Session
            userService.Login(user);
            ContextHelper.getSession().put(Naming.SessionName.UserSession, new UserInfo(user.getUserId(), user.getUserName(), user.getFullName(), user.getRole().getRoleId(), user.getRole().getRoleName()));
            json.add(Json.Answer, Json.Informantion);
            json.add(Json.MsgTitle, messageSource.getMessage("message.defaultMsgTitle", null,Locale.getDefault()));
            json.add(Json.MsgContent, messageSource.getMessage("user.loginSuccess", null,Locale.getDefault()));
        }
        else 
        {
            json.add(Json.Answer, false);
            json.add(Json.MsgTitle, messageSource.getMessage("message.errorMsgTitle", null,Locale.getDefault()));
            json.add(Json.MsgContent, messageSource.getMessage("user.pswError", null,Locale.getDefault()));
        }
        message=json.toString();
        return SUCCESS;
    }

    public String getMessage() {
        return message;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String username) {
        this.userName = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }
}
